import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/goods",
  },
  //进入页面重定向到goods
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/index",
    name: "index",
    component: () => import("../views/Index.vue"),
    children: [
      {
        path: "/goods",
        name: "goods",
        component: () => import("../views/Goods.vue"),
        meta: {
          title: "商品列表",
        },
        //给路由加描述 可以是中文的
      },
      {
        path: "/order",
        name: "order",
        component: () => import("../views/Order.vue"),
        meta: {
          title: "订单管理",
        },
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (localStorage.getItem("token")) {
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
});
//导航守卫

export default router;
